package com.mes.cloud.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mes.cloud.api.dto.request.role.BatchEnableRoleRequest;
import com.mes.cloud.api.dto.request.role.SysRoleEnablePageListRequest;
import com.mes.cloud.api.dto.request.role.SysRolePageListRequest;
import com.mes.cloud.api.dto.request.role.SysRoleRequest;
import com.mes.cloud.api.dto.response.role.SysRoleDto;
import com.mes.cloud.exception.BaseException;
import com.mes.cloud.common.CommPager;
import com.mes.cloud.common.JsonResult;
import com.mes.cloud.constant.Constants;
import com.mes.cloud.dao.SysEmployeeRoleDao;
import com.mes.cloud.dao.SysRoleDao;
import com.mes.cloud.entity.SysEmployeeRole;
import com.mes.cloud.entity.SysRole;
import com.mes.cloud.enums.HttpStatusEnum;
import com.mes.cloud.util.DateUtil;
import com.mes.cloud.util.UserUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色表
 *
 * @author wxm
 * @version 2019-08-01 19:31:54
 * @email xiaomin.wu1@pactera.com
 */
@Service
public class SysRoleService {

    private SysRoleDao sysRoleDao;

    private SysEmployeeRoleDao sysEmployeeRoleDao;

    private SysCodeGeneratorService sysCodeGeneratorService;

    private IPKeyGenerator iPKeyGenerator;

    @Autowired
    public SysRoleService(SysRoleDao sysRoleDao, SysEmployeeRoleDao sysEmployeeRoleDao, SysCodeGeneratorService sysCodeGeneratorService, IPKeyGenerator iPKeyGenerator) {
        this.sysRoleDao = sysRoleDao;
        this.sysEmployeeRoleDao = sysEmployeeRoleDao;
        this.sysCodeGeneratorService = sysCodeGeneratorService;
        this.iPKeyGenerator = iPKeyGenerator;
    }

    /**
     * 分页查列表
     *
     * @param sysRolePageListRequest
     * @return JsonResult
     */
    public JsonResult<CommPager<SysRoleDto>> pageList(SysRolePageListRequest sysRolePageListRequest) {
        Page<SysRoleDto> page = PageHelper.startPage(sysRolePageListRequest.getCurrentPage(), sysRolePageListRequest.getPageSize());
        //查询条件
        List<SysRole> sysRoleList = sysRoleDao.list(sysRolePageListRequest);
        List<SysRoleDto> sysRoleDtoList = new ArrayList<>();
        for (SysRole sysRole : sysRoleList) {
            SysRoleDto sysRoleDto = new SysRoleDto();
            BeanUtils.copyProperties(sysRole, sysRoleDto);
            sysRoleDtoList.add(sysRoleDto);
        }
        CommPager<SysRoleDto> pageResult = new CommPager<>(page, sysRoleDtoList);
        JsonResult<CommPager<SysRoleDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(pageResult);
        return jsonResult;
    }

    /**
     * 新增
     *
     * @param sysRoleRequest
     * @return JsonResult
     */
    public JsonResult insert(SysRoleRequest sysRoleRequest) {
        JsonResult jsonResult = new JsonResult();

        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleRequest, sysRole);
        sysRole.setCode(sysCodeGeneratorService.getCode(Constants.CODE_GENERATOR_ROLE));
        sysRole.setId(iPKeyGenerator.getGenerateKey());
        sysRole.setUpdateDateStr(DateUtil.updateDateStr());
        sysRole.setCreateBy(UserUtil.getCreateByName());
        sysRole.setUpdateBy(UserUtil.getUpdateByName());
        sysRole.setCreateByNo(UserUtil.getCreateByNo());
        sysRole.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = sysRoleDao.insertSelective(sysRole);
        if (count > 0) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 更新
     *
     * @param sysRoleRequest
     * @return JsonResult
     */
    public JsonResult update(SysRoleRequest sysRoleRequest) {
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleRequest, sysRole);
        sysRole.setUpdateDateStr(DateUtil.updateDateStr());
        sysRole.setUpdateBy(UserUtil.getUpdateByName());
        sysRole.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = sysRoleDao.updateByPrimaryKeySelective(sysRole);
        JsonResult jsonResult = new JsonResult();
        if (count == 1) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 删除
     *
     * @param id
     * @return JsonResult
     */
    public JsonResult delete(String id) {
        SysRole sysRole = new SysRole();
        sysRole.setId(id);
        sysRole.setDelFlag(Constants.DEL_FLAG_YES);
        sysRole.setUpdateDateStr(DateUtil.updateDateStr());
        sysRole.setUpdateBy(UserUtil.getUpdateByName());
        sysRole.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = sysRoleDao.updateByPrimaryKeySelective(sysRole);
        JsonResult jsonResult = new JsonResult();
        if (count == 1) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 查看详情
     *
     * @param id
     * @return JsonResult
     */
    public JsonResult<SysRoleDto> get(String id) {
        JsonResult<SysRoleDto> jsonResult = new JsonResult();
        SysRole sysRole = sysRoleDao.selectByPrimaryKey(id);
        if (sysRole == null) {
            return jsonResult.setCode(HttpStatusEnum.RECORD_NOT_EXIST.getCode()).setMsg(HttpStatusEnum.RECORD_NOT_EXIST.getMessage());
        }
        SysRoleDto sysRoleDto = new SysRoleDto();
        BeanUtils.copyProperties(sysRole, sysRoleDto);
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(sysRoleDto);
        return jsonResult;
    }

    /**
     * 禁用启用
     *
     * @param id
     * @param enableFlag
     * @return JsonResult
     */
    public JsonResult updateEnableFlag(String id, String enableFlag) {
        JsonResult jsonResult = new JsonResult();
        //如果是禁用，需要判断角色是否被引用，如果被引用不允许禁用
        if (Constants.ENABLE_FLAG_NO.equals(enableFlag)) {
            //角色员工关联表
            SysEmployeeRole sysEmployeeRoleParam = new SysEmployeeRole();
            sysEmployeeRoleParam.setDelFlag(Constants.DEL_FLAG_NO);
            sysEmployeeRoleParam.setRoleId(id);
            int countTemp = sysEmployeeRoleDao.selectCount(sysEmployeeRoleParam);
            //若果被引用，返回错误提示
            if (countTemp > 0) {
                throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
            }
        }
        SysRole sysRole = new SysRole();
        sysRole.setId(id);
        sysRole.setEnableFlag(enableFlag);
        sysRole.setUpdateDateStr(DateUtil.updateDateStr());
        sysRole.setUpdateBy(UserUtil.getUpdateByName());
        sysRole.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = sysRoleDao.updateByPrimaryKeySelective(sysRole);

        if (count == 1) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }

    /**
     * 批量禁用启用
     *
     * @param batchEnableRoleRequest
     * @return
     */
    public JsonResult batchEnableFlag(BatchEnableRoleRequest batchEnableRoleRequest) {
        JsonResult jsonResult = new JsonResult();
        //如果是禁用，需要判断角色是否被引用，如果被引用不允许禁用
        if (Constants.ENABLE_FLAG_NO.equals(batchEnableRoleRequest.getEnableFlag())) {
            //角色ID
            List<String> ids = batchEnableRoleRequest.getIds();
            //与用户有关联关系的角色ID，返回前端
            List<String> hasRefIds = new ArrayList<>();
            int countTemp = 0;
            SysEmployeeRole sysEmployeeRoleParam = new SysEmployeeRole();
            sysEmployeeRoleParam.setDelFlag(Constants.DEL_FLAG_NO);
            for (String id : ids) {
                sysEmployeeRoleParam.setRoleId(id);
                //判断角色下是否有用户
                countTemp = sysEmployeeRoleDao.selectCount(sysEmployeeRoleParam);
                if (countTemp > 0) {
                    hasRefIds.add(id);
                }
            }
            //如果角色被用户关联，返回被关联的角色
            if (hasRefIds.size() > 0) {
                jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
                jsonResult.setCode(HttpStatusEnum.OK.getCode());
                jsonResult.setData(hasRefIds);
                return jsonResult;
            }
        }
        int count = sysRoleDao.batchEnableFlag(batchEnableRoleRequest.getEnableFlag(), batchEnableRoleRequest.getIds(),
                DateUtil.updateDateStr(), UserUtil.getEmployeeNo(), UserUtil.getName());
        if (count > 0) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }

        return jsonResult;
    }

    /**
     * 分页查列表
     *
     * @param sysRoleEnablePageListRequest
     * @return JsonResult
     */
    public JsonResult<CommPager<SysRoleDto>> enablePageList(SysRoleEnablePageListRequest sysRoleEnablePageListRequest) {
        Page<SysRoleDto> page = PageHelper.startPage(sysRoleEnablePageListRequest.getCurrentPage(), sysRoleEnablePageListRequest.getPageSize());
        //查询条件
        List<SysRole> sysRoleList = sysRoleDao.enablePageList(sysRoleEnablePageListRequest);
        List<SysRoleDto> sysRoleDtoList = new ArrayList<>();
        for (SysRole sysRole : sysRoleList) {
            SysRoleDto sysRoleDto = new SysRoleDto();
            BeanUtils.copyProperties(sysRole, sysRoleDto);
            sysRoleDtoList.add(sysRoleDto);
        }
        CommPager<SysRoleDto> pageResult = new CommPager<>(page, sysRoleDtoList);
        JsonResult<CommPager<SysRoleDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(pageResult);
        return jsonResult;
    }
}
